import { createI18n } from 'vue-i18n'
import { Local } from '@/utils/storage'
import { LANG } from '@/stores/constant/cacheKey'

// element-plus国际化
import enLocale from 'element-plus/dist/locale/en.mjs'
import zhLocale from 'element-plus/dist/locale/zh-cn.mjs'
import zhTwLocale from 'element-plus/dist/locale/zh-tw.mjs'
import viLocale from 'element-plus/dist/locale/vi.mjs'
import koLocale from 'element-plus/dist/locale/ko.mjs'
import jaLocale from 'element-plus/dist/locale/ja.mjs'
import thLocale from 'element-plus/dist/locale/th.mjs'
import idLocale from 'element-plus/dist/locale/id.mjs'
import frLocale from 'element-plus/dist/locale/fr.mjs'
import deLocale from 'element-plus/dist/locale/de.mjs'
import ruLocale from 'element-plus/dist/locale/ru.mjs'
// import msLocale from 'element-plus/dist/locale/ms.mjs'
// import hiLocale from 'element-plus/dist/locale/hi.mjs'

function siphonI18n(prefix = 'zh-CN') {
    return Object.fromEntries(
        Object.entries(import.meta.glob('./lang/*/index.ts', { eager: true })).map(([key, value]: any) => {
            const matched = key.match(/([A-Za-z0-9-_]+)\/index/i)[1]
            return [matched, value.default]
        })
    )[prefix]
}

export const localesConfigs = {
    'zh-CN': {
        ...siphonI18n('zh-CN'),
        ...zhLocale,
    },
    en: {
        ...siphonI18n('en'),
        ...enLocale,
    },
    vi: {
        ...siphonI18n('vi'),
        ...viLocale,
    },
    'zh-TW': {
        ...siphonI18n('zh-TW'),
        ...zhTwLocale,
    },
    ko: {
        ...siphonI18n('ko'),
        ...koLocale,
    },
    ja: {
        ...siphonI18n('ja'),
        ...jaLocale,
    },
    th: {
        ...siphonI18n('th'),
        ...thLocale,
    },
    id: {
        ...siphonI18n('id'),
        ...idLocale,
    },
    fr: {
        ...siphonI18n('fr'),
        ...frLocale,
    },
    de: {
        ...siphonI18n('de'),
        ...deLocale,
    },
    ru: {
        ...siphonI18n('ru'),
        ...ruLocale,
    },
}

// 获取当前环境的国际化语言
export const getCurrentLanguage = () => {
    return (Local.get(LANG) === 'cn' ? 'zh-CN' : Local.get(LANG)) || navigator.language || 'zh-CN'
}

export const i18n = createI18n({
    allowComposition: true,
    // 如果要支持 compositionAPI，此项必须设置为 false
    legacy: false,
    // 设置语言类型
    globalInjection: true,
    locale: getCurrentLanguage(),
    messages: localesConfigs,
    // 解决控制台报错
    silentTranslationWarn: true,
    missingWarn: false,
    silentFallbackWarn: true,
    fallbackWarn: false,
})

const { t } = i18n.global
export const $t = t

// 设置语言环境
export const setLang = (lang: string) => {
    i18n.global.locale.value = lang
    Local.set(LANG, lang)
}

// 判断当前是否是中文环境
export const isChinese = () => {
    return judgeLanguage('zh-CN')
}

// 判断当前是否是环境
export const isEnglish = () => {
    return judgeLanguage('en')
}

// 根据code判断当前语言环境
const judgeLanguage = (code: string) => {
    return getCurrentLanguage() === code
}

// 将本地语言code转换为后端语言code
export const transApiLang = (localLang: string) => {
    switch (localLang) {
        case 'zh-CN':
            return 'ch'
        case 'zh-TW':
            return 'zh-tw'
    }
    return localLang
}

// 将后端语言code转换为本地code
export const transLocalLang = (apiLang: string) => {
    switch (apiLang) {
        case 'ch':
            return 'zh-CN'
        case 'zh-tw':
            return 'zh-TW'
    }
    return apiLang
}
